package com.firetornado.android.gowallajava;

import java.util.List;

import com.firetornado.android.gowallajava.ImageOps;
import com.firetornado.android.gowallajava.GowallaJavaAuth;
import com.firetornado.android.gowallajava.R;
import com.ginsberg.gowalla.dto.LocatedSpot;
import com.ginsberg.gowalla.dto.Trip;
import com.ginsberg.gowalla.exception.GowallaException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * This is an example of how to use gowalla-java within Android to call and display trip details.
 * 
 * @author Ryan Crutchfield - rc6750@gmail.com
 * http://gowalla.com/users/rc6750
 *
 */

public class TripsMain extends Activity {
	final Handler mHandler = new Handler();
	Trip tripDetail = null;
	
	@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.tripsmain);
        final ProgressDialog pd = ProgressDialog.show(this,"Trip Details","Loading....",true, false);
   		new Thread(new Runnable(){
       		public void run(){
       			try {
       	        	tripDetail = GowallaJavaAuth.gowalla.getTrip(66);
       			} catch (GowallaException e) {
       				// TODO Auto-generated catch block
       				e.printStackTrace();
       			}
       	    
       			mHandler.post(mUpdateResults);
        		pd.dismiss();
       		}
   		}).start();
    }
	
	final Runnable mUpdateResults = new Runnable() {
        public void run() {
            updateResultsInUi(tripDetail);
        }
    }; 
    
    final void updateResultsInUi(Trip tripDetail)
    {
    	Drawable image = ImageOps.ImageOperations(tripDetail.getImageUrl(),"image.jpg");		
    	ImageView imgView = (ImageView)findViewById(R.id.tripImage);
		imgView.setImageDrawable(image);
    			
		TextView spotName = (TextView)findViewById(R.id.tripName);
		spotName.setText(tripDetail.getName());
		
		TextView spotDescription = (TextView)findViewById(R.id.tripDesc);
		spotDescription.setText(tripDetail.getDescription());
		
		TextView details = (TextView)findViewById(R.id.tripDetails);
		details.setText("\nCreated by " + tripDetail.getCreator().getName() + "\n"
				+ Integer.toString(tripDetail.getCompletedUsersCount()) + " users have completed this trip.\n\n" 
				+ "Spots:\n");
		
		List<LocatedSpot> tripSpots = tripDetail.getSpots();
		for (LocatedSpot spot : tripSpots)
		{
			details.append(spot.getName()+"\n");
		}
    }	
	
}